//
//  daemonize.cpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <iostream>
#include "logging/logging.hpp"


int daemonize()
{
  pid_t pid;
  int fd;
  if(getppid()==1) return 0;
  pid=fork();
  if (pid<0)
  {
    LOG(ERROR)<<"Error fork";;
  }
  if (pid>0) exit(0);
  setsid();
  setpgid(0,0);
  for (fd=getdtablesize();fd>=0;--fd) close(fd);
  fd=open("/dev/null",O_RDWR);
  dup(fd);
  dup(fd);
  umask(027); 
  chdir("/");
  LOG(INFO)<<"Daemonized";;
  return 0;
}
